<template>
  <div class="bottom-bar">
    <div class="check-area">
      <check-button class="all-checked" :is-checked="isSelectAll" @click.native="allSelect"></check-button>
      <span @click="allSelect">全选</span>
    </div>
    <div class="price">
      合计:{{totalPrice}}
    </div>
    <div class="calculate">
      去计算:({{checkLength}})
    </div>
  </div>
</template>

<script>
  import CheckButton from "../../../components/content/checkbutton/CheckButton";
  import {mapGetters} from 'vuex'
  export default {
    name: "CartBottomBar",
    components: {
      CheckButton
    },
    computed: {
      ...mapGetters(['cartList']),
      totalPrice() {
        return '￥' + this.$store.state.cartList.filter(item => {
          return item.checked
        }).reduce((preValue, item) => {
          return preValue + parseFloat(item.price) * item.count
        }, 0)
      },
      checkLength() {
        return this.$store.state.cartList.filter(item => {
          return item.checked
        }).length
      },
      isSelectAll() {
        // return !(this.$store.state.cartList.filter(item => !item.checked).length)
        if (this.$store.state.cartList.length === 0 ){
          return false
        }
        return !(this.$store.state.cartList.find(item => !item.checked))
      }
    },
    methods:{
      allSelect() {
        if(this.isSelectAll) {
          this.cartList.forEach(item => item.checked = false)
        } else {
          this.cartList.forEach(item => item.checked = true)
        }
      }
    }
  }
</script>

<style scoped>
  .bottom-bar {
    display: flex;
    height: 40px;
    background-color: #eee;
    bottom: 40px;
    line-height: 40px;
    font-size: 14px;
  }
  .check-area {
    display: flex;
    align-items: center;
    margin-left: 10px;
  }
  .all-checked {
    width: 20px;
    height: 20px;
    line-height: 20px;
    margin-right: 5px;
  }
  .price {
    margin-left: 10px;
  }
  .calculate {
    position: absolute;
    right: 0;
    background-color: red;
    width: 35%;
    text-align: center;
    border-radius: 5px;
  }
</style>
